package whu.cn.whushare.ui.common;

import whu.cn.whushare.R;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.androidplus.util.StringUtil;

//在LoadingHelper类中包含对登陆成功后的内容页面、登录进行时的页面和重新登录页面的布局文件控制，
// 提供一些供外部调用的显示当前内容、loading页面、retry页面的方法
public class LoadingHelper {

    public static enum LOADING_STATE {
        PARSE_Fail, SUCCESS, NETWORK_ERROR, NO_RESULT, LOADING
    }

    private View mContentView;
    private LinearLayout mRetryLayout;
    private ImageView mRetryImage;
    private Button mRetryButton;
    private LinearLayout mLoadingLayout;
    private Context mContext;

    private View.OnClickListener mRetryClick;

    public LoadingHelper(View.OnClickListener retryClick) {
        mRetryClick = retryClick;
    }

    /**
     * 创建布局文件，将inflater生成的布局文件添加到content所在的父控件的指定位置
     * @param inflater - 布局文件生成器
     * @param content - 通过content获得布局文件的父控件
     */
    public void onCreateView(LayoutInflater inflater, View content) {
        ViewGroup parent = (ViewGroup) content.getParent();
        addViews(inflater, parent, parent.indexOfChild(content));
        showContentView();
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState, View content) {
        View result;
        mContext = inflater.getContext();
        if (content.getParent() != null) {
            ViewGroup parent = (ViewGroup) content.getParent();
            addViews(inflater, parent, parent.indexOfChild(content));
            result = parent;
        } else {
            View view = inflater.inflate(R.layout.common_net_interaction_layout, container, false);
            mRetryLayout = (LinearLayout) view.findViewById(R.id.common_retry_layout);
            mRetryImage = (ImageView) view.findViewById(R.id.common_retry_image);
            mRetryButton = (Button) mRetryLayout.findViewById(R.id.common_retry_tv);
            mLoadingLayout = (LinearLayout) view.findViewById(R.id.common_loading_layout);
            //containerLayout是一个layout容器，其中包含retryLayout和loadingLayout
            LinearLayout containerLayout = (LinearLayout) view.findViewById(R.id.common_interaction_container);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT);
            containerLayout.addView(content, layoutParams);
            mContentView = content;
            mRetryButton.setOnClickListener(mRetryClick);
            mRetryLayout.setOnClickListener(mRetryClick);
            result = view;
        }
        return result;
    }

    //将通过inflater生成的loadingView和retryView添加到父控件parent的position位置
    private void addViews(LayoutInflater inflater, ViewGroup parent, int position) {
        mContentView = parent.getChildAt(position);
        mContentView.setVisibility(View.GONE);

        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        View loadingView = inflater.inflate(R.layout.common_loading_layout, null, false);
        mLoadingLayout = (LinearLayout) loadingView.findViewById(R.id.common_loading_layout);
        parent.addView(loadingView, position, params);

        View retryView = inflater.inflate(R.layout.common_retry_layout, null, false);
        mRetryLayout = (LinearLayout) retryView.findViewById(R.id.common_retry_layout);
        mRetryImage = (ImageView) retryView.findViewById(R.id.common_retry_image);
        parent.addView(retryView, position, params);
        mRetryLayout.setVisibility(View.GONE);
        mRetryButton = (Button) mRetryLayout.findViewById(R.id.common_retry_tv);
        mRetryLayout.setOnClickListener(mRetryClick);
        mRetryButton.setOnClickListener(mRetryClick);
    }

    //判断retryView是否可见，若可见，返回true；若不可见，返回false；
    public boolean isRetryViewVisible() {
        return mRetryLayout.getVisibility() == View.VISIBLE;
    }

    //显示当前内容视图
    public void showContentView() {
        mContentView.setVisibility(View.VISIBLE);
        mLoadingLayout.setVisibility(View.GONE);
        mRetryLayout.setVisibility(View.GONE);
    }

    /**
     * 显示loading视图
     * @param isLoadMore - 为true时，不作处理；为false时，将loading视图设置为正在加载中
     */
    public void showLoadingView(boolean isLoadMore) {
        if (!isLoadMore) {
            mContentView.setVisibility(View.GONE);
            mLoadingLayout.setVisibility(View.VISIBLE);
            mRetryLayout.setVisibility(View.GONE);
        }
    }

    //显示Retry视图
    public void showRetryView() {
        showRetryImage(true);
        showRetryView(null);
    }

    public void showRetryImage(boolean show) {
        showRetryImage(show, R.drawable.network_error);
    }

    public void showRetryImage(boolean show, int imageResId) {
        if (show) {
            mRetryImage.setImageResource(imageResId);
            mRetryImage.setVisibility(View.VISIBLE);
        } else
            mRetryImage.setVisibility(View.GONE);
    }

    public void showRetryView(String text) {
        showRetryView(text, R.drawable.network_error, true);
    }

    public void showRetryView(String text, int imageResId, boolean isShow) {
        showRetryImage(isShow, imageResId);
        if (!StringUtil.isNullOrEmpty(text)) {
            mRetryButton.setText(text);
        }

        mContentView.setVisibility(View.GONE);
        mLoadingLayout.setVisibility(View.GONE);
        mRetryLayout.setVisibility(View.VISIBLE);
    }
}
